using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces;

namespace SheLife.Infrastructure.Repositories
{
    public class SkincareRepository : ISkincareRepository
    {
        private readonly SheLifeDbContext _context;

        public SkincareRepository(SheLifeDbContext context)
        {
            _context = context;
        }

        public async Task<Skincare> GetByIdAsync(int id)
        {
            return await _context.Skincare.FindAsync(id);
        }

        public async Task<IEnumerable<Skincare>> GetByUserIdAsync(int userId)
        {
            return await _context.Skincare
                .Where(s => s.UserId == userId)
                .OrderByDescending(s => s.CreatedAt)
                .ToListAsync();
        }

        public async Task<IEnumerable<Skincare>> GetByUserIdAndCategoryAsync(int userId, string category)
        {
            return await _context.Skincare
                .Where(s => s.UserId == userId && s.Category == category)
                .OrderByDescending(s => s.CreatedAt)
                .ToListAsync();
        }

        public async Task<IEnumerable<Skincare>> GetSkincareWithProductsByUserIdAsync(int userId)
        {
            return await _context.Skincare
                .Include(s => s.SkincareProducts)
                .ThenInclude(sp => sp.Product)
                .Where(s => s.UserId == userId)
                .OrderByDescending(s => s.CreatedAt)
                .ToListAsync();
        }

        public async Task<Skincare> AddAsync(Skincare skincare)
        {
            skincare.CreatedAt = DateTime.UtcNow;
            await _context.Skincare.AddAsync(skincare);
            await _context.SaveChangesAsync();
            return skincare;
        }

        public async Task UpdateAsync(Skincare skincare)
        {
            skincare.UpdatedAt = DateTime.UtcNow;
            _context.Entry(skincare).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }

        public async Task DeleteAsync(int id)
        {
            var skincare = await GetByIdAsync(id);
            if (skincare != null)
            {
                _context.Skincare.Remove(skincare);
                await _context.SaveChangesAsync();
            }
        }

        public async Task<bool> ExistsAsync(int id)
        {
            return await _context.Skincare.AnyAsync(s => s.Id == id);
        }

        public async Task<Dictionary<string, int>> GetCategoryStatsAsync(int userId)
        {
            return await _context.Skincare
                .Where(s => s.UserId == userId)
                .GroupBy(s => s.Category)
                .Select(g => new { Category = g.Key, Count = g.Count() })
                .ToDictionaryAsync(g => g.Category, g => g.Count);
        }

        public async Task<int> GetTotalSkincareCountAsync(int userId)
        {
            return await _context.Skincare
                .CountAsync(s => s.UserId == userId);
        }
    }
}